package com.ming;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws IOException, TemplateException {
        // 1.创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        // 2.设置模板所在的目录
        String path = "D:\\Program Files\\Git\\repo\\daily-learn\\freemarker\\src\\main\\resources\\ftl";//Thread.currentThread().getContextClassLoader().getResource("").getPath();
        System.out.println(path);
        configuration.setDirectoryForTemplateLoading(new File(path));
        // 3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        // 4.加载模板
        Template template = configuration.getTemplate("test.ftl");
        // 5. 创建数据模型
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","zs");
        map.put("message","欢迎来到...");
        map.put("success", true);

        List list=new ArrayList<>();
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("name","A");
        map1.put("price","1");
        list.add(map1);
        HashMap<Object, Object> map2 = new HashMap<>();
        map2.put("name","B");
        map2.put("price","2");
        list.add(map2);
        map.put("list",list);

        map.put("a",null);
        map.put("b","b");

        List<Object> list1 = new ArrayList<>();
        Map<String,String> map3 = new HashMap<>();
        map3.put("name",null);
        map3.put("age","12");


        Map<String,String> map4 = new HashMap<>();
        map4.put("name","ls");
        map4.put("age","13");
        list1.add(map3);
        list1.add(map4);
        list1.add(null);
        map.put("list1",list1);

        // 6. 创建Writer对象
        FileWriter writer = new FileWriter(new File(path+"/test.html"));
        // 7. 输出
        template.process(map,writer);
        // 8. 关闭流
        writer.close();
    }
}
